package ca.swank.hardquor;

import javax.swing.*;

/**
* An advanced <CODE>JTextArea</CODE> component for displaying a real
* time sequence of one line messages. As new messages are added, old
* messages scroll out and are not visible. For this reason, instances
* of the <CODE>hardquorSmartTextArea</CODE> should have a sufficient
* number of rows.
*
* @author Jesse Wilson
* @version 1.0 9/23/2001
*/
public class hardquorSmartTextArea extends JTextArea
{
  private int[] commentLengths;
  private StringBuffer commentText;
  private int commentCurrentFirst;

  /**
  * creates a new <CODE>hardquorSmartTextArea</CODE> with the specified number
  * of rows and columns. The component holds one message for each row, with
  * old messages being replaced as new ones are added.
  *
  * @param columns the number of columns (width) of the text area.
  * @param rows the number of one line messages that will be displayed at
  *        any time by the text area.
  */
  public hardquorSmartTextArea(int columns, int rows)
  { super(rows, columns);

    commentLengths = new int[rows];
    for(int i = 0; i < commentLengths.length; i++)
    { commentLengths[i] = 0;
    }
    commentCurrentFirst = 0;
    commentText = new StringBuffer("");
    commentLengths[commentCurrentFirst] = 1;
  }

  /**
  * appends a one line message to the current <CODE>hardquorSmartTextArea</CODE>.
  *
  * @param text a new message to append to the current series of messages.
  *        Note that new messages will cause old messages to scroll out.
  *        Messages may not contain new line characters.
  */
  public void writeln(String text)
  { commentText.append("\n").append(text);
	commentText.delete(0, commentLengths[commentCurrentFirst]);
	commentLengths[commentCurrentFirst] = text.length() + 1;
	commentCurrentFirst = (commentCurrentFirst + 1) % commentLengths.length;
	setText(commentText.toString());
  }

}

